package com.qk.controller;

import com.qk.Dto.CustomerDto;
import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Customer;
import com.qk.server.CustomerServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerServer customerServer;
    //查寻客户列表
    @GetMapping
    public Result findCustomerByList( CustomerDto customerDto){
        log.info("查询客户列表");
        PageResult<Customer> customer = customerServer.findCustomerByList(customerDto);
        return Result.success(customer);
    }
    //添加客户
    @Log
    @PostMapping
    public Result addCustomer(@RequestBody Customer customer){
        log.info("添加客户");
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerServer.save(customer);
        return Result.success();
    }
    //根据Id查询客户回显
    @GetMapping("/{id}")
    public Result findCustomerById(@PathVariable Integer id){
        log.info("查询客户id:{}",id);
        Customer customer = customerServer.getById(id);
        return Result.success(customer);
    }
    //修改客户
    @Log
    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        log.info("修改客户");
        customer.setUpdateTime(LocalDateTime.now());
        customerServer.updateById(customer);
        return Result.success();
    }
}
